{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "801dc95e",
   "metadata": {},
   "source": [
    "1.题目: 定义三个变量 a, b, c，并分别将整数 1, 2, 3 赋值给它们。然后，使用 print() 函数将每个变量的值单独打印出来，每个值占一行。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "06c50330",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "a=1\n",
    "b=1 \n",
    "c=1\n",
    "\n",
    "print(a)\n",
    "print(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9ba80b4b",
   "metadata": {},
   "source": [
    "进阶：使用换行符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a5ca447f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "print(a,b,c,sep='\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e74c5b5",
   "metadata": {},
   "source": [
    "2.题目: 创建两个变量：name 存储你的名字（字符串，例如 \"小明\"），city 存储你所在的城市（字符串，例如 \"北京\"）。使用 f-string 将这两个变量组合成一句话并打印出来。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1b16f918",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是岳海川来自东营\n",
      "接下来分行输出：\n",
      "我的名字是岳海川\n",
      "我来自东营\n"
     ]
    }
   ],
   "source": [
    "name='岳海川'\n",
    "city='东营'\n",
    "print(f\"我是{name}来自{city}\")\n",
    "print(f\"接下来分行输出：\")\n",
    "print(f\"我的名字是{name}\\n我来自{city}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd7ae09e",
   "metadata": {},
   "source": [
    "3.题目: 定义两个整数变量，num1 赋值为 20，num2 赋值为 8。计算这两个变量的和，并将结果存储在一个新的变量 a 中；计算这两个变量的商，叫做b；计算这两个变量的余数，叫做c。然后，使用 f-string 打印出类似 “20 加 8 的结果是：28” 的信息，分成三行打印。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4629263d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20+8的结果是28\n",
      "20/8的结果是2.5\n",
      "20%8的结果是4\n"
     ]
    }
   ],
   "source": [
    "num1=20\n",
    "num2=8\n",
    "a=num1 + num2\n",
    "b=num1 / num2\n",
    "c=num1 % num2\n",
    "print(f\"20+8的结果是{a}\")\n",
    "print(f\"20/8的结果是{b}\")\n",
    "print(f\"20%8的结果是{c}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7a8eb8d",
   "metadata": {},
   "source": [
    "4.题目: 定义两个浮点数变量，price 赋值为 19.9，discount 赋值为 0.8 (表示 8 折)。计算折扣后的价格，并将结果存储在变量 final_price 中；计算节省了多少钱，存储在变量 saved_amount 中。然后，使用 f-string 分两行打印出类似以下格式的信息：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ca406b09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最终价格是15.92\n",
      "节省了3.98\n"
     ]
    }
   ],
   "source": [
    "price=19.9\n",
    "discount=0.8\n",
    "final_price=price*discount\n",
    "saved_amount=price-final_price\n",
    "print(f'最终价格是{final_price:.2f}\\n节省了{saved_amount:.2f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2633941b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "format()方法\n",
      "保留三位小数：{:.3f}.format(final_amount)\n",
      "保留五位小数：{:.5f}.format(saved_amount)\n",
      "-------------------\n",
      "舍入函数round()\n",
      "保留三位小数15.92\n"
     ]
    }
   ],
   "source": [
    "print(f'format()方法')\n",
    "print(\"保留三位小数：{:.3f}.format(final_amount)\")\n",
    "print(\"保留五位小数：{:.5f}.format(saved_amount)\")\n",
    "print(\"-------------------\")\n",
    "print(\"舍入函数round()\")\n",
    "rounded_num=round(final_price,3)\n",
    "# 错误写法：print(\"保留三位小数”,rounded_num)\n",
    "# rounded_num还是浮点型\n",
    "#正确写法：\n",
    "print(f\"保留三位小数{rounded_num}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pylearn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
